<?php

namespace app\controller\system;

use app\controller\BaseController;
use app\model\system\SysConfig;
use support\Request;
use support\Response;

class ConfigController extends BaseController
{

    public function create(Request $request): Response
    {
        $data = $request->post();
        $config = SysConfig::create($data);
        if (!$config) {
            return $this->error('创建失败');
        } else {
            return $this->success([]);
        }
    }

    public function form(int $id): Response
    {
        $log = SysConfig::find($id);
        if (!$log) {
            return $this->error('数据不存在');
        }
        return $this->success($log->getFormArray());
    }


    public function update(Request $request, int $id): Response
    {
        $data = $request->post();
        $config = SysConfig::find($id);

        if (!$config) {
            return $this->error('数据不存在');
        }

        if ($config->update($data)) {
            return $this->success($config->getFormArray());
        } else {
            return $this->error('更新失败');
        }
    }

    public function delete(int $id): Response
    {
        $config = SysConfig::find($id);
        if (!$config) {
            return $this->error('数据不存在');
        }
        if (!$config->delete()) {
            return $this->error('删除失败');
        } else {
            return $this->success($config->toArray());
        }
    }

    public function page(Request $request): Response
    {
        $params = $request->get();
        extract($params);
        $list = SysConfig::order("id", "desc")->paginate(['list_rows' => $pageSize, 'page' => $pageNum]);
        $total = $list->total();
        return $this->success(["list" => $list->items(), "total" => $total]);
    }

    public function refresh(): Response{
        
        return $this->success([]);
    }
}
